package io.github.mcsdk.ui.util;

/**
 * Layout calculation utilities for responsive UI design
 *
 * @author MC SDK Team
 * @version 1.0.0
 * @since 1.0.0
 */
public class LayoutUtil {

    /**
     * Calculates position to center an element
     *
     * @param containerSize Container size
     * @param elementSize Element size
     * @return Centered position
     */
    public static int center(int containerSize, int elementSize) {
        return (containerSize - elementSize) / 2;
    }

    /**
     * Calculates X position to center horizontally
     *
     * @param containerX Container X
     * @param containerWidth Container width
     * @param elementWidth Element width
     * @return Centered X position
     */
    public static int centerX(int containerX, int containerWidth, int elementWidth) {
        return containerX + center(containerWidth, elementWidth);
    }

    /**
     * Calculates Y position to center vertically
     *
     * @param containerY Container Y
     * @param containerHeight Container height
     * @param elementHeight Element height
     * @return Centered Y position
     */
    public static int centerY(int containerY, int containerHeight, int elementHeight) {
        return containerY + center(containerHeight, elementHeight);
    }

    /**
     * Clamps a value between min and max
     *
     * @param value Value to clamp
     * @param min Minimum value
     * @param max Maximum value
     * @return Clamped value
     */
    public static int clamp(int value, int min, int max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * Clamps a float value between min and max
     *
     * @param value Value to clamp
     * @param min Minimum value
     * @param max Maximum value
     * @return Clamped value
     */
    public static float clamp(float value, float min, float max) {
        return Math.max(min, Math.min(max, value));
    }

    /**
     * Checks if a point is within a rectangle
     *
     * @param pointX Point X
     * @param pointY Point Y
     * @param rectX Rectangle X
     * @param rectY Rectangle Y
     * @param rectWidth Rectangle width
     * @param rectHeight Rectangle height
     * @return true if point is inside rectangle
     */
    public static boolean isPointInRect(int pointX, int pointY, int rectX, int rectY,
                                         int rectWidth, int rectHeight) {
        return pointX >= rectX && pointX < rectX + rectWidth &&
               pointY >= rectY && pointY < rectY + rectHeight;
    }

    /**
     * Calculates responsive size based on screen dimensions
     *
     * @param screenSize Screen dimension (width or height)
     * @param minSize Minimum size
     * @param maxSize Maximum size
     * @param percentage Percentage of screen (0.0 to 1.0)
     * @return Calculated size
     */
    public static int responsiveSize(int screenSize, int minSize, int maxSize, float percentage) {
        int calculated = (int) (screenSize * percentage);
        return clamp(calculated, minSize, maxSize);
    }

    /**
     * Distributes elements evenly in a container
     *
     * @param containerSize Container size
     * @param elementCount Number of elements
     * @param elementSize Size of each element
     * @return Array of positions for each element
     */
    public static int[] distributeEvenly(int containerSize, int elementCount, int elementSize) {
        if (elementCount <= 0) return new int[0];
        if (elementCount == 1) return new int[]{center(containerSize, elementSize)};

        int totalElementSize = elementCount * elementSize;
        int totalGap = containerSize - totalElementSize;
        int gap = totalGap / (elementCount + 1);

        int[] positions = new int[elementCount];
        for (int i = 0; i < elementCount; i++) {
            positions[i] = gap + i * (elementSize + gap);
        }
        return positions;
    }

    /**
     * Calculates grid layout positions
     *
     * @param containerWidth Container width
     * @param containerHeight Container height
     * @param columns Number of columns
     * @param rows Number of rows
     * @param cellWidth Cell width
     * @param cellHeight Cell height
     * @param gapX Horizontal gap
     * @param gapY Vertical gap
     * @return 2D array of [x, y] positions
     */
    public static int[][] gridLayout(int containerWidth, int containerHeight,
                                      int columns, int rows,
                                      int cellWidth, int cellHeight,
                                      int gapX, int gapY) {
        int[][] positions = new int[rows * columns][2];

        int totalWidth = columns * cellWidth + (columns - 1) * gapX;
        int totalHeight = rows * cellHeight + (rows - 1) * gapY;

        int startX = center(containerWidth, totalWidth);
        int startY = center(containerHeight, totalHeight);

        int index = 0;
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < columns; col++) {
                positions[index][0] = startX + col * (cellWidth + gapX);
                positions[index][1] = startY + row * (cellHeight + gapY);
                index++;
            }
        }

        return positions;
    }

    /**
     * Linear interpolation between two values
     *
     * @param start Start value
     * @param end End value
     * @param t Interpolation factor (0.0 to 1.0)
     * @return Interpolated value
     */
    public static float lerp(float start, float end, float t) {
        return start + (end - start) * clamp(t, 0, 1);
    }

    /**
     * Easing function for smooth animations (ease-in-out)
     *
     * @param t Time parameter (0.0 to 1.0)
     * @return Eased value (0.0 to 1.0)
     */
    public static float easeInOut(float t) {
        t = clamp(t, 0, 1);
        return t < 0.5f ? 2 * t * t : 1 - (float) Math.pow(-2 * t + 2, 2) / 2;
    }
}